< previous page page_669 next page >

Page 669
These are the form-level variables you need to perform the update.
Add the following code, which creates the workspace and opens the database. Create a new Sub called OpenDB and place the following code in the routine:
Sub OpenDB()
    On Error GoTo OpenDBErr
    '
    nErrFlag = 0 ' assume all is OK
    '
    Set wsUpdate = DBEngine.CreateWorkspace("wsUpdate", "admin", "")
    Set db = wsUpdate.OpenDatabase("C:\TYSDBVB5\SOURCE\DATA\MULTIUS4.MDB", True)
    '
    GoTo OpenDBExit
    '
OpenDBErr:
    MsgBox Trim(Str(Err)) + " " + Error$(Err), vbCritical, "OpenDB"
    nErrFlag = Err
    '
OpenDBExit:
    '
End Sub
This routine creates a new workspace object to encompass the transaction and then opens the database for exclusive use. You don't want anyone else in the system while you perform this major update. An error-trap routine has been added here in case you can't open the database exclusively.
Now you can add the code that performs the actual month-end update. Do this by using the SQL statements you learned in the lessons on Days 13, "Creating Databases with SQL," and 15, "Updating Databases with SQL." Create a new Sub called ProcMonthEnd and then add the following code:
Sub ProcMonthEnd()
    On Error goto ProcMonthEndErr
    '
    Dim cSQL As String
    Dim nResult As Integer
    '
    wsUpdate.BeginTrans ' mark start of transaction
    '
    ' append totals to transtotals table
    cSQL = "INSERT INTO TransTotals SELECT TransTable.CustID,
    _SUM(TransTable.Amount) as Amount FROM TransTable
    _GROUP BY TransTable.CustID"
    db.Execute cSQL
    '
    ' append history records
    cSQL = "INSERT INTO TransHistory SELECT * FROM TransTable"
    db.Execute cSQL
    '
    ' delete the transaction records
    cSQL = "DELETE FROM TransTable"
    db.Execute cSQL
    '

 
< previous page page_669 next page >

If you like this book, buy it!